package org.luxor.common.netty.config;

import org.luxor.common.netty.TcpNioServer;
import org.luxor.common.netty.config.properties.NettyServerProperties;
import org.luxor.common.netty.protocol.NettyServerPipelineConfig;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Netty 服务端全局配置
 *
 * @author Mr.Yan  @date: 2022/04/06
 */
@Configuration
@EnableConfigurationProperties(NettyServerProperties.class)
@ConditionalOnProperty(prefix = NettyServerProperties.PREFIX, name = "enabled", havingValue = "true")
public class NettyServerAutoConfig {

    @Bean
    @ConditionalOnMissingBean(TcpNioServer.class)
    public TcpNioServer tcpNioServer(NettyServerProperties nettyServerProperties) {
        // 启动服务
        TcpNioServer tcpNioServer = new TcpNioServer(new NettyServerPipelineConfig(nettyServerProperties));
        tcpNioServer.start();
        return tcpNioServer;
    }

}
